<template>
  <!-- 分配权限的弹层 -->
  <el-dialog
    title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
    :visible="showAssignDialog"
    @close="closeAssignDialog"
  >
    <!-- 权限点数据展示 -->
    <el-tree
      ref="tree"
      :data="list"
      :props="{label:'name'}"
      node-key="id"
      show-checkbox
      check-strictly
    ></el-tree>
    <template #footer>
      <el-button @click="closeAssignDialog">取消</el-button>
      <el-button type="primary" @click="saveperm">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getPermissionList } from '@/api/permisson'
import { transformTreeData } from '@/utils/index'
import { getRoleDetail, assignPerm } from '@/api/setting'
export default {
  props: {
    showAssignDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      list: [],
      user: ''
    }
  },
  created () {
    this.getPermissionList()
  },
  methods: {
    // 分配权限点确定
    async saveperm () {
      await assignPerm({ id: this.user, permIds: this.$refs.tree.getCheckedKeys() })
      this.$message.success('分配成功')
      this.$emit('update:showAssignDialog')
    },
    // 获取角色的权限点
    async getRoleDetail (id) {
      const data = await getRoleDetail(id)
      console.log(data.data.permIds)
      this.$refs.tree.setCheckedKeys(data.data.permIds)
      this.user = id
    },
    // 获取列表
    async getPermissionList () {
      const data = await getPermissionList()
      console.log(data.data)
      this.list = transformTreeData(data.data)
    },

    // 关闭触发
    closeAssignDialog () {
      // 重置树选中为空：[]
      this.$refs.tree.setCheckedKeys([])
      this.$emit('update:showAssignDialog', false)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
